package ch15;

import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;

/**
 * Created by zhangyunqi on 2017/10/15.
 */

    public class Codes
    {
        /**
         * Encode and decode a message using a key of values stored in
         * a queue.
         */
        public static void main ( String[] args)
        {
            int[] key = {5, 12, -3, 8, -9, 4, 10};
            Integer keyValue;
            String encoded = "", decoded = "";
            String message = "All programmers are playwrights and all " +
                    "computers are lousy actors.";

            Deque<Integer> keyQueue1 = new Deque <Integer> () {
                @Override
                public void addFirst(Integer integer) {


                }

                @Override
                public void addLast(Integer integer) {

                }

                @Override
                public boolean offerFirst(Integer integer) {
                    return false;
                }

                @Override
                public boolean offerLast(Integer integer) {
                    return false;
                }

                @Override
                public Integer removeFirst() {
                    return null;
                }

                @Override
                public Integer removeLast() {
                    return null;
                }

                @Override
                public Integer pollFirst() {
                    return null;
                }

                @Override
                public Integer pollLast() {
                    return null;
                }

                @Override
                public Integer getFirst() {
                    return null;
                }

                @Override
                public Integer getLast() {
                    return null;
                }

                @Override
                public Integer peekFirst() {
                    return null;
                }

                @Override
                public Integer peekLast() {
                    return null;
                }

                @Override
                public boolean removeFirstOccurrence(Object o) {
                    return false;
                }

                @Override
                public boolean removeLastOccurrence(Object o) {
                    return false;
                }

                @Override
                public boolean add(Integer integer) {
                    return false;
                }

                @Override
                public boolean offer(Integer integer) {
                    return false;
                }

                @Override
                public Integer remove() {
                    return null;
                }

                @Override
                public Integer poll() {
                    return null;
                }

                @Override
                public Integer element() {
                    return null;
                }

                @Override
                public Integer peek() {
                    return null;
                }

                @Override
                public void push(Integer integer) {

                }

                @Override
                public Integer pop() {
                    return null;
                }

                @Override
                public boolean remove(Object o) {
                    return false;
                }

                @Override
                public boolean containsAll(Collection<?> c) {
                    return false;
                }

                @Override
                public boolean addAll(Collection <? extends Integer> c) {
                    return false;
                }

                @Override
                public boolean removeAll(Collection <?> c) {
                    return false;
                }

                @Override
                public boolean retainAll(Collection <?> c) {
                    return false;
                }

                @Override
                public void clear() {

                }

                @Override
                public boolean equals(Object o) {
                    return false;
                }

                @Override
                public int hashCode() {
                    return 0;
                }

                @Override
                public boolean contains(Object o) {
                    return false;
                }

                @Override
                public int size() {
                    return 0;
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public Iterator<Integer> iterator() {
                    return null;
                }

                @Override
                public Object[] toArray() {
                    return new Object[0];
                }

                @Override
                public <T> T[] toArray(T[] a) {
                    return null;
                }

                @Override
                public Iterator <Integer> descendingIterator() {
                    return null;
                }
            };
            Deque<Integer> keyQueue2 = new Deque <Integer> () {
                @Override
                public void addFirst(Integer integer) {

                }

                @Override
                public void addLast(Integer integer) {

                }

                @Override
                public boolean offerFirst(Integer integer) {
                    return false;
                }

                @Override
                public boolean offerLast(Integer integer) {
                    return false;
                }

                @Override
                public Integer removeFirst() {
                    return null;
                }

                @Override
                public Integer removeLast() {
                    return null;
                }

                @Override
                public Integer pollFirst() {
                    return null;
                }

                @Override
                public Integer pollLast() {
                    return null;
                }

                @Override
                public Integer getFirst() {
                    return null;
                }

                @Override
                public Integer getLast() {
                    return null;
                }

                @Override
                public Integer peekFirst() {
                    return null;
                }

                @Override
                public Integer peekLast() {
                    return null;
                }

                @Override
                public boolean removeFirstOccurrence(Object o) {
                    return false;
                }

                @Override
                public boolean removeLastOccurrence(Object o) {
                    return false;
                }

                @Override
                public boolean add(Integer integer) {

                    return false;
                }

                @Override
                public boolean offer(Integer integer) {
                    return false;
                }

                @Override
                public Integer remove() {
                    return null;
                }

                @Override
                public Integer poll() {
                    return null;
                }

                @Override
                public Integer element() {
                    return null;
                }

                @Override
                public Integer peek() {
                    return null;
                }

                @Override
                public void push(Integer integer) {

                }

                @Override
                public Integer pop() {
                    return null;
                }

                @Override
                public boolean remove(Object o) {
                    return false;
                }

                @Override
                public boolean containsAll(Collection <?> c) {
                    return false;
                }

                @Override
                public boolean addAll(Collection <? extends Integer> c) {
                    return false;
                }

                @Override
                public boolean removeAll(Collection <?> c) {
                    return false;
                }

                @Override
                public boolean retainAll(Collection <?> c) {
                    return false;
                }

                @Override
                public void clear() {

                }

                @Override
                public boolean equals(Object o) {
                    return false;
                }

                @Override
                public int hashCode() {
                    return 0;
                }

                @Override
                public boolean contains(Object o) {
                    return false;
                }

                @Override
                public int size() {
                    return 0;
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public Iterator <Integer> iterator() {
                    return null;
                }

                @Override
                public Iterator <Integer> descendingIterator() {
                    return null;
                }

                @Override
                public Object[] toArray() {
                    return new Object[0];
                }

                @Override
                public <T> T[] toArray(T[] a) {
                    return null;
                }


            };

            for (int scan=0; scan < key.length; scan++)
            {
                keyQueue1.addLast (new Integer(key[scan]));
                keyQueue2.addLast (new Integer(key[scan]));
            }

            for (int scan=0; scan < message.length(); scan++)
            {
                keyValue = keyQueue1.removeFirst ();
                encoded += (char) ((int)message.charAt(scan) + keyValue.intValue());
                keyQueue1.addLast (keyValue);
            }

            System.out.println ("Encoded Message:\n" + encoded + "\n");

            for (int scan=0; scan < encoded.length(); scan++)
            {
                keyValue = keyQueue2.removeFirst ();
                decoded += (char) ((int)encoded.charAt(scan) - keyValue.intValue());
                keyQueue2.addLast (keyValue);
            }

            System.out.println ("Decoded Message:\n" + decoded);
        }
    }

